package com.pdg.islandinterloper.database.objects;

import java.util.ArrayList;
import java.util.List;


public class WorldPosition {
	private static final double DEFAULT_X = 0;
	private static final double DEFAULT_Y = 0;
	private double x = DEFAULT_X;
	private double y = DEFAULT_Y;
	public WorldPosition(double theX,double theY){
		setX(theX);
		setY(theY);
	}
	public WorldPosition() {
	}
	public double getX() {
		return x;
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return y;
	}
	public void setY(double y) {
		this.y = y;
	}
	private double getCartesianDistance2(WorldPosition theOther){
		return (theOther.getX()-getX())*(theOther.getX()-getX())+(theOther.getY()-getY())*(theOther.getY()-getY());
	}
	public double getCartesianDistance(WorldPosition theOther){
		return Math.sqrt(getCartesianDistance2(theOther));
	}
	public double getWorldShapeDistance(WorldPosition theOther,Profile theProfile){
		List<Double> theDistances = new ArrayList<Double>();
		boolean selfLeftEdge = getX()<theProfile.getIslandSpacing();
		boolean selfTopEdge = getY()<theProfile.getIslandSpacing();
		boolean selfRightEdge = getX()>=theProfile.getWorldWidth()-theProfile.getIslandSpacing();
		boolean selfBottomEdge = getY()>=theProfile.getWorldHeight()-theProfile.getIslandSpacing();
		boolean otherLeftEdge = theOther.getX()<theProfile.getIslandSpacing();
		boolean otherTopEdge = theOther.getY()<theProfile.getIslandSpacing();
		boolean otherRightEdge = theOther.getX()>=theProfile.getWorldWidth()-theProfile.getIslandSpacing();
		boolean otherBottomEdge = theOther.getY()>=theProfile.getWorldHeight()-theProfile.getIslandSpacing();
		theDistances.add(getCartesianDistance2(theOther));
		if(theProfile.getWorldWrapX()){
			if(selfLeftEdge && otherRightEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()-theProfile.getWorldWidth(),theOther.getY())));
			}
			if(selfRightEdge && otherLeftEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()+theProfile.getWorldWidth(),theOther.getY())));
			}
		}
		if(theProfile.getWorldWrapY()){
			if(selfTopEdge && otherBottomEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX(),theOther.getY()-theProfile.getWorldHeight())));
			}
			if(selfBottomEdge && otherTopEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX(),theOther.getY()+theProfile.getWorldHeight())));
			}
		}
		if(theProfile.getWorldWrapX() && theProfile.getWorldWrapX()){
			if(selfLeftEdge && selfTopEdge && otherRightEdge && otherBottomEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()-theProfile.getWorldWidth(),theOther.getY()-theProfile.getWorldHeight())));
			}
			if(selfRightEdge && selfTopEdge && otherLeftEdge && otherBottomEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()+theProfile.getWorldWidth(),theOther.getY()-theProfile.getWorldHeight())));
			}
			if(selfLeftEdge && selfBottomEdge && otherRightEdge && otherTopEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()-theProfile.getWorldWidth(),theOther.getY()+theProfile.getWorldHeight())));
			}
			if(selfRightEdge && selfBottomEdge && otherLeftEdge && otherTopEdge){
				theDistances.add(getCartesianDistance2(new WorldPosition(theOther.getX()+theProfile.getWorldWidth(),theOther.getY()+theProfile.getWorldHeight())));
			}
		}
		double theResult = theDistances.get(0);
		for(Double theDistance:theDistances){
			if(theDistance<theResult){
				theResult = theDistance;
			}
		}
		return Math.sqrt(theResult);
	}
}
